/*
 * @Author: your name
 * @Date: 2021-09-20 19:45:02
 * @LastEditTime: 2021-09-21 20:12:01
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \重构项目\briup-ej-app\src\store\foreignList.js
 */
export default {
  namespaced: true,
  state: {
    epidemicData:[]
  },
  getters: {
    //返回全球的疫情数据json
    foreignList(state) {
        // JSON.parse(state.epidemicData.data)
      let foreignObj  = state.epidemicData.foreignList
      let arrNew = []
      foreignObj.forEach(function(item){
          let obj = {}
          let {name,confirm} = item
          obj.name = name
          obj.value = confirm
          arrNew.push(obj)
      });
      let obj1 = {name:'中国',value:'124113'}
      arrNew.push(obj1)
      let obj2 = {name:'格陵兰岛',value:'464'}
      arrNew.push(obj2)
      return arrNew
    },
    //现存确诊
    foreignNowList(state) {
      // JSON.parse(state.epidemicData.data)
    let foreignObj  = state.epidemicData.foreignList
    let arrNew = []
    foreignObj.forEach(function(item){
        let obj = {}
        let {name,nowConfirm} = item
        obj.name = name
        obj.value = nowConfirm
        arrNew.push(obj)
    });
    let obj1 = {name:'中国',value:'124113'}
    arrNew.push(obj1)
    let obj2 = {name:'格陵兰岛',value:'464'}
    arrNew.push(obj2)
    return arrNew
  },
    //统计累计
    total(state) {
      // { "nowConfirm": 46987153, "confirm": 217021468, "heal": 165524088, 
      // "dead": 4510227, "nowConfirmAdd": 0, "confirmAdd": 0, "healAdd": 0, "deadAdd": 0, 
      // "lastUpdateTime": "2021-09-20 09:34:36" } 
      let epidemic = new Map()
      let {nowConfirm,confirm,heal,dead} = state.epidemicData.globalStatis
      let healRate = (heal/confirm).toFixed(3)
      let deadRate = (dead/confirm).toFixed(2)
      epidemic.set('nowConfirm',nowConfirm)
      epidemic.set('confirm',confirm)
      epidemic.set('heal',heal)
      epidemic.set('dead',dead)
      epidemic.set('healRate',healRate)
      epidemic.set('deadRate',deadRate)
      return epidemic
    }
  },
  mutations:{
     SET_DATA(state,DataList){
       state.epidemicData = JSON.parse(DataList.data)
     }   
  },
  actions:{
    getForeignData({commit},payload){
      let res = payload
      commit('SET_DATA',res)
    }
  }
}